package com.rslai.base.tool.servertest.command;

import com.rslai.base.tool.servertest.context.Context;
import com.rslai.base.tool.servertest.response.Response;
import com.rslai.base.tool.servertest.util.CloneUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoopStepCommand extends CompositeStepCommand {
    private int times;

    public LoopStepCommand(int times, List<StepCommand> children) {
        super(children);
        this.times = times;
    }

    public Response doExecute(Response preResult, Context context) throws Throwable {
        Response response = preResult;
        for (long i = 0L; i < this.times; i += 1L) {
            response = super.doExecute(preResult, context);
        }
        return response;
    }

    public StepCommand doClone() {
        return new LoopStepCommand(this.times, CloneUtil.cloneStepCommand(this.children));
    }

    public Map<String, Object> toReport() {
        Map details = new HashMap();
        details.put("stepName", "执行:");
        details.put("name", "循环执行" + String.valueOf(this.times) + "次command" + getChildren());
        List params = new ArrayList();
        details.put("params", params);
        return details;
    }
}
